package com.ute.chinesechess;

import android.util.Log;

public class VanCo {
	public static NguoiChoi[] NguoiChoi = new NguoiChoi[2];
	public static String TenNguoiChoi1;
	public static String TenNguoiChoi2;
	public static boolean DangChoi = false;
	public static int LuotDi = 0;
	public static int winner = 2;

	public static void KT() {
		NguoiChoi[0] = new NguoiChoi(0);
		NguoiChoi[1] = new NguoiChoi(1);

		// Phe 0 Quan Do
		BanCo.datQuanCo("TUONG", 0, 0, 0, 4);
		BanCo.datQuanCo("SY", 0, 0, 0, 3);
		BanCo.datQuanCo("SY", 0, 1, 0, 5);
		BanCo.datQuanCo("TINH", 0, 0, 0, 2);
		BanCo.datQuanCo("TINH", 0, 1, 0, 6);
		BanCo.datQuanCo("XE", 0, 0, 0, 0);
		BanCo.datQuanCo("XE", 0, 1, 0, 8);
		BanCo.datQuanCo("PHAO", 0, 0, 2, 1);
		BanCo.datQuanCo("PHAO", 0, 1, 2, 7);
		BanCo.datQuanCo("MA", 0, 0, 0, 1);
		BanCo.datQuanCo("MA", 0, 1, 0, 7);
		BanCo.datQuanCo("TOT", 0, 0, 3, 0);
		BanCo.datQuanCo("TOT", 0, 1, 3, 2);
		BanCo.datQuanCo("TOT", 0, 2, 3, 4);
		BanCo.datQuanCo("TOT", 0, 3, 3, 6);
		BanCo.datQuanCo("TOT", 0, 4, 3, 8);

		// Phe 1 Quan Den
		BanCo.datQuanCo("TUONG", 1, 0, 9, 4);
		BanCo.datQuanCo("SY", 1, 0, 9, 3);
		BanCo.datQuanCo("SY", 1, 1, 9, 5);
		BanCo.datQuanCo("TINH", 1, 0, 9, 2);
		BanCo.datQuanCo("TINH", 1, 1, 9, 6);
		BanCo.datQuanCo("XE", 1, 0, 9, 0);
		BanCo.datQuanCo("XE", 1, 1, 9, 8);
		BanCo.datQuanCo("PHAO", 1, 0, 7, 1);
		BanCo.datQuanCo("PHAO", 1, 1, 7, 7);
		BanCo.datQuanCo("MA", 1, 0, 9, 1);
		BanCo.datQuanCo("MA", 1, 1, 9, 7);
		BanCo.datQuanCo("TOT", 1, 0, 6, 0);
		BanCo.datQuanCo("TOT", 1, 1, 6, 2);
		BanCo.datQuanCo("TOT", 1, 2, 6, 4);
		BanCo.datQuanCo("TOT", 1, 3, 6, 6);
		BanCo.datQuanCo("TOT", 1, 4, 6, 8);
	}

	public static void Khoa(boolean phe) {
		int i = 0;
		NguoiChoi[i].qTuong.TrangThai = phe;
		NguoiChoi[i].qSy[0].TrangThai = phe;
		NguoiChoi[i].qSy[1].TrangThai = phe;
		NguoiChoi[i].qTinh[0].TrangThai = phe;
		NguoiChoi[i].qTinh[1].TrangThai = phe;
		NguoiChoi[i].qXe[0].TrangThai = phe;
		NguoiChoi[i].qXe[1].TrangThai = phe;
		NguoiChoi[i].qPhao[0].TrangThai = phe;
		NguoiChoi[i].qPhao[1].TrangThai = phe;
		NguoiChoi[i].qMa[0].TrangThai = phe;
		NguoiChoi[i].qMa[1].TrangThai = phe;
		NguoiChoi[i].qTot[0].TrangThai = phe;
		NguoiChoi[i].qTot[1].TrangThai = phe;
		NguoiChoi[i].qTot[2].TrangThai = phe;
		NguoiChoi[i].qTot[3].TrangThai = phe;
		NguoiChoi[i].qTot[4].TrangThai = phe;
		i = 1;
		if (phe == true)
			phe = false;
		else
			phe = true;
		NguoiChoi[i].qTuong.TrangThai = phe;
		NguoiChoi[i].qSy[0].TrangThai = phe;
		NguoiChoi[i].qSy[1].TrangThai = phe;
		NguoiChoi[i].qTinh[0].TrangThai = phe;
		NguoiChoi[i].qTinh[1].TrangThai = phe;
		NguoiChoi[i].qXe[0].TrangThai = phe;
		NguoiChoi[i].qXe[1].TrangThai = phe;
		NguoiChoi[i].qPhao[0].TrangThai = phe;
		NguoiChoi[i].qPhao[1].TrangThai = phe;
		NguoiChoi[i].qMa[0].TrangThai = phe;
		NguoiChoi[i].qMa[1].TrangThai = phe;
		NguoiChoi[i].qTot[0].TrangThai = phe;
		NguoiChoi[i].qTot[1].TrangThai = phe;
		NguoiChoi[i].qTot[2].TrangThai = phe;
		NguoiChoi[i].qTot[3].TrangThai = phe;
		NguoiChoi[i].qTot[4].TrangThai = phe;
	}

	public static boolean TuongAnToan(int Phe) {
		int i;
		int j;

		i = VanCo.NguoiChoi[Phe].qTuong.Hang;
		j = VanCo.NguoiChoi[Phe].qTuong.Cot;

		int cot0;
		int hang0;
		int cot1;
		int hang1;

		cot0 = VanCo.NguoiChoi[0].qTuong.Cot;
		hang0 = VanCo.NguoiChoi[0].qTuong.Hang;
		cot1 = VanCo.NguoiChoi[1].qTuong.Cot;
		hang1 = VanCo.NguoiChoi[1].qTuong.Hang;
		
		if(Phe == 0)
			Phe = 1;
		else
			Phe = 0;
		
		if (VanCo.NguoiChoi[Phe].qMa[0].Song == true
				&& VanCo.NguoiChoi[Phe].qMa[0].KiemTra(i, j) == true)
			return false;
		if (VanCo.NguoiChoi[Phe].qMa[1].Song == true
				&& VanCo.NguoiChoi[Phe].qMa[1].KiemTra(i, j) == true)
			return false;
		if (VanCo.NguoiChoi[Phe].qXe[0].Song == true
				&& VanCo.NguoiChoi[Phe].qXe[0].KiemTra(i, j) == true)
			return false;
		if (VanCo.NguoiChoi[Phe].qXe[1].Song == true
				&& VanCo.NguoiChoi[Phe].qXe[1].KiemTra(i, j) == true)
			return false;
		if (VanCo.NguoiChoi[Phe].qPhao[0].Song == true
				&& VanCo.NguoiChoi[Phe].qPhao[0].KiemTra(i, j) == true)
			return false;
		if (VanCo.NguoiChoi[Phe].qPhao[1].Song == true
				&& VanCo.NguoiChoi[Phe].qPhao[1].KiemTra(i, j) == true)
			return false;
		if (VanCo.NguoiChoi[Phe].qTot[0].Song == true
				&& VanCo.NguoiChoi[Phe].qTot[0].KiemTra(i, j) == true)
			return false;
		if (VanCo.NguoiChoi[Phe].qTot[1].Song == true
				&& VanCo.NguoiChoi[Phe].qTot[1].KiemTra(i, j) == true)
			return false; 
		if (VanCo.NguoiChoi[Phe].qTot[2].Song == true
				&& VanCo.NguoiChoi[Phe].qTot[2].KiemTra(i, j) == true)
			return false;
		if (VanCo.NguoiChoi[Phe].qTot[3].Song == true
				&& VanCo.NguoiChoi[Phe].qTot[3].KiemTra(i, j) == true)
			return false;
		if (VanCo.NguoiChoi[Phe].qTot[4].Song == true
				&& VanCo.NguoiChoi[Phe].qTot[4].KiemTra(i, j) == true)
			return false;
		/*
		 * Kiem tra sau khi thuc hien buoc di tuong co bi doi mat khong
		 */
		if (cot0 == cot1) {
			for (int k = hang0 + 1; k < hang1; k++) {
				if (BanCo.board[k][cot0].Trong == false) {
					return true;
				}
			}
			return false;
		} else
			return true;
	}

	public static boolean ChieuBi(int Phe) {
		if (VanCo.TuongAnToan(Phe) == false) {
			for (int i = 0; i <= 9; i++) {
				for (int j = 0; j <= 8; j++) {
					//Log.d(""+i+" "+j,""+VanCo.NguoiChoi[Phe].qTuong.Move(i, j));
					if (VanCo.NguoiChoi[Phe].qTuong.Song == true
							&& VanCo.NguoiChoi[Phe].qTuong.Move(i, j) == true)
						return false;
					if (VanCo.NguoiChoi[Phe].qSy[0].Song == true
							&& VanCo.NguoiChoi[Phe].qSy[0].Move(i, j) == true)
						return false;
					if (VanCo.NguoiChoi[Phe].qSy[1].Song == true
							&& VanCo.NguoiChoi[Phe].qSy[1].Move(i, j) == true)
						return false;
					if (VanCo.NguoiChoi[Phe].qTinh[0].Song == true
							&& VanCo.NguoiChoi[Phe].qTinh[0].Move(i, j) == true)
						return false;
					if (VanCo.NguoiChoi[Phe].qTinh[1].Song == true
							&& VanCo.NguoiChoi[Phe].qTinh[1].Move(i, j) == true)
						return false;
					if (VanCo.NguoiChoi[Phe].qXe[0].Song == true
							&& VanCo.NguoiChoi[Phe].qXe[0].Move(i, j) == true)
						return false;
					if (VanCo.NguoiChoi[Phe].qXe[1].Song == true
							&& VanCo.NguoiChoi[Phe].qXe[1].Move(i, j) == true)
						return false;
					if (VanCo.NguoiChoi[Phe].qPhao[0].Song == true
							&& VanCo.NguoiChoi[Phe].qPhao[0].Move(i, j) == true)
						return false;
					if (VanCo.NguoiChoi[Phe].qPhao[1].Song == true
							&& VanCo.NguoiChoi[Phe].qPhao[1].Move(i, j) == true)
						return false;
					if (VanCo.NguoiChoi[Phe].qMa[0].Song == true
							&& VanCo.NguoiChoi[Phe].qMa[0].Move(i, j) == true)
						return false;
					if (VanCo.NguoiChoi[Phe].qMa[1].Song == true
							&& VanCo.NguoiChoi[Phe].qMa[1].Move(i, j) == true)
						return false;
					if (VanCo.NguoiChoi[Phe].qTot[0].Song == true
							&& VanCo.NguoiChoi[Phe].qTot[0].Move(i, j) == true)
						return false;
					if (VanCo.NguoiChoi[Phe].qTot[1].Song == true
							&& VanCo.NguoiChoi[Phe].qTot[1].Move(i, j) == true)
						return false;
					if (VanCo.NguoiChoi[Phe].qTot[2].Song == true
							&& VanCo.NguoiChoi[Phe].qTot[2].Move(i, j) == true)
						return false;
					if (VanCo.NguoiChoi[Phe].qTot[3].Song == true
							&& VanCo.NguoiChoi[Phe].qTot[3].Move(i, j) == true)
						return false;
					if (VanCo.NguoiChoi[Phe].qTot[4].Song == true
							&& VanCo.NguoiChoi[Phe].qTot[4].Move(i, j) == true)
						return false;
				}
			}
			return true;
		}
		return false;
	}
}
